/**
 * 
 */
package com.lz.ovuola.util.tomcat;

import java.io.File;

import org.apache.catalina.connector.Connector;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.http.HttpStatus;

/**
 * 编程方式自定义内嵌容器
 * 
 * @author fz
 *
 */
@SuppressWarnings("deprecation")
// @Configuration
// @PropertySource("classpath:/tomcat.https.properties")
// @EnableConfigurationProperties(CustomTomcatEmbeddedCustomizer.TomcatSslConnectorProperties.class)
public class CustomTomcatEmbeddedCustomizer {

	/**
	 * 默认页面
	 * 
	 * @param container
	 */
	protected void addErrorPages(ConfigurableEmbeddedServletContainer container) {
		ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED,
				"/401.html");
		ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND,
				"/404.html");
		ErrorPage error500Page = new ErrorPage(
				HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
		container.addErrorPages(error401Page, error404Page, error500Page);
	}

	/**
	 * 订制内嵌tomcat容器
	 */
	// @Bean
	public EmbeddedServletContainerFactory servletContainer(
			TomcatSslConnectorProperties properties) {
		TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
		// factory.setPort(9000);
		// factory.setSessionTimeout(10, TimeUnit.MINUTES);
		factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
				"/notfound.html"));
		factory.addAdditionalTomcatConnectors(createSslConnector(properties));
		return factory;
	}

	private Connector createSslConnector(TomcatSslConnectorProperties properties) {
		System.out.println("TomcatSslConnectorProperties----------"
				+ properties.getScheme()
				+ properties.getKeystore().getAbsolutePath()
				+ properties.getKeystorePassword());
		Connector connector = new Connector();
		properties.configureConnector(connector);
		return connector;
	}

	/**
	 * 
	 * https支持
	 *
	 */
	// @ConfigurationProperties(prefix = "custom.tomcat.https")
	public static class TomcatSslConnectorProperties {
		private Integer port;
		private Boolean ssl = true;
		private Boolean secure = true;
		private String scheme = "https";
		private File keystore;
		private String keystorePassword;

		public void configureConnector(Connector connector) {
			if (port != null) {
				connector.setPort(port);
			}
			if (secure != null) {
				connector.setSecure(secure);
			}
			if (scheme != null) {
				connector.setScheme(scheme);
			}
			if (ssl != null) {
				connector.setProperty("SSLEnabled", ssl.toString());
			}
			if (keystore != null && keystore.exists()) {
				connector.setProperty("keystoreFile",
						keystore.getAbsolutePath());
				System.out.println("keystoreFile-------------------"
						+ keystore.getAbsolutePath());
				// connector.setProperty("keystorePassword", keystorePassword);
				connector.setProperty("keystorePass", keystorePassword);

			}
		}

		public Integer getPort() {
			return port;
		}

		public void setPort(Integer port) {
			this.port = port;
		}

		public Boolean getSsl() {
			return ssl;
		}

		public void setSsl(Boolean ssl) {
			this.ssl = ssl;
		}

		public Boolean getSecure() {
			return secure;
		}

		public void setSecure(Boolean secure) {
			this.secure = secure;
		}

		public String getScheme() {
			return scheme;
		}

		public void setScheme(String scheme) {
			this.scheme = scheme;
		}

		public File getKeystore() {
			return keystore;
		}

		public void setKeystore(File keystore) {
			this.keystore = keystore;
		}

		public String getKeystorePassword() {
			return keystorePassword;
		}

		public void setKeystorePassword(String keystorePassword) {
			this.keystorePassword = keystorePassword;
		}
	}

}